package Function;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Vector;

import Function.Utils.FCFileCopyToMaterial;
import VWorldGameStudio.FCBlock;
import VWorldGameStudio.FCMap;
import lombok.Getter;
import utils.FCDataDelete;
import utils.FCDataSingles;
import utils.FCFileUrl;

public class FCMapLibrary {
	@Getter
	private Vector<FCMap> mapList = new Vector<FCMap>();
	private Vector<String> mapName = new Vector<String>();
	private FCFileCopyToMaterial fileCopy = new FCFileCopyToMaterial();
	private FCDataDelete deleter = FCDataSingles.getDeleter();
	private static final String MAP_LIST_PATH = "./data/maplist.vw";
	private static final String MAPS_PATH = "./data/map";
	private int mapallnum=0;//地图实际数量
	/**
	 * 初始化地图类，读取地图数量
	 * 建议游戏启动方使用
	 * @date 2021-11-30
	 */
	public FCMapLibrary() {
		try {
			this.mapallnum=new File(MAPS_PATH).listFiles().length;
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	/** 
	 * 地图数据加载函数
	 * @date 2021-11-30
	 */
	public void loadMapData() {
		try {
			File[] f=new File(MAPS_PATH).listFiles();
			for(int i=0;i<mapallnum;i++) {
				FCMap map = FCDataSingles.getReader().readMapData(i,f[i].getPath());
				mapList.add(map);
				mapName.add(map.getName());
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获取地图实际数据量
	 * @return integer
	 * @date 2021-11-30
	 */
	public int getMapAllNum() {
		return this.mapallnum;
	}
	/**
	 * 初始化地图
	 * 
	 * @param isGame
	 */
	public FCMapLibrary(boolean isGame) {
		try {
//			BufferedReader reader = new BufferedReader(new FileReader(new File(MAP_LIST_PATH)));
//			String maps = reader.readLine();
//			int i = 0;
			File[] f=new File(MAPS_PATH).listFiles();
			for(int i=0;i<f.length;i++) {
				FCMap map = FCDataSingles.getReader().readMapData(i,f[i].getPath());
				mapList.add(map);
				mapName.add(map.getName());
//				maps = reader.readLine();
			}
//			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 新建地图
	public void createMap(String name, int x, int y, String info, String url) {
		FCMap m = new FCMap();
		m.createMap(name, x, y, info, fileCopy.copyPngToMaterialPng(0, url));
		mapList.add(m);
		mapName.add(name);
	}

	// 获取npc根据地图名
	public HashMap<String, String> getNPCOfName(String name) {
		return mapList.get(mapName.indexOf(name)).getMapNpcHashMap();
	}

	// 获取地图
	public FCMap getMapOfName(String name) {
		return mapList.get(mapName.indexOf(name));
	}

	// 获取地图名称
	public String getMapNameOfID(int ID) {
		return mapName.get(ID);
	}

	// 获取地图数量
	public int getMapNum() {
		return (mapName == null) ? 0 : mapName.size();
	}

	// 检测是否重复
	public boolean isMapRepeat(String name) {
		for (int i = 0; i < mapName.size(); i++)
			if (mapName.get(i).equals(name))
				return true;
		return false;
	}

	// 删除地图
	public boolean deleteMapIsOK(String name) {
		if (deleter.deleteData(FCFileUrl.URL_DATA_MAP + name + ".vw")) {
			mapList.remove(mapName.indexOf(name));
			mapName.remove(name);
			return true;
		} else
			return false;
	}
}
